﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Pyramid.Models;
using Newtonsoft.Json;

namespace Pyramid.Common
{
	public class MenuHelper
	{
		public static MvcHtmlString GenerateMenu4DwzFramework(List<WebObject> webObjects)
		{
			var html = new StringBuilder();
			var modules = webObjects.Where(x => x.WebObjectType == (int) WebObjectTypes.Module).OrderBy(x => x.OrderWeight).ToList();
			foreach (var module in modules)
			{
				html.AppendFormat("<div class=\"accordionHeader\"><h2><span>Folder</span>{0}</h2></div>", module.Name);

				html.Append("<div class=\"accordionContent\"><ul class=\"tree treeFolder\">");
				var menus = webObjects.Where(x => x.ParentId == module.Id).OrderBy(x => x.OrderWeight).ToList();
				foreach (var menu in menus)
				{
					var subItems = webObjects
						.Where(x => x.ParentId == menu.Id && x.WebObjectType == (int) WebObjectTypes.Menu)
						.OrderBy(x => x.OrderWeight)
						.ToList();
					if (subItems.Count > 0)
					{
						html.AppendFormat("<li><a>{0}</a>", menu.Name);
						GenerateSubMenu(subItems, webObjects, html);
						html.Append("</li>");
					}
					else
					{
						html.AppendFormat("<li><a href=\"{0}\" target=\"{1}\" rel=\"tab_{2}\">{3}</a>", menu.Href, menu.Target, menu.Id.ToLower(),
										menu.Name);
						html.Append("</li>");
					}
				}
				html.Append("</ul></div>");
			}
			return MvcHtmlString.Create(html.ToString());
		}

		protected static void GenerateSubMenu(List<WebObject> menus, List<WebObject> source, StringBuilder html)
		{
			html.Append("<ul>");
			foreach (var menu in menus)
			{
				var subItems = source.Where(x => x.ParentId == menu.Id && x.WebObjectType == (int) WebObjectTypes.Menu).OrderBy(x => x.OrderWeight).ToList();
				if (subItems.Count > 0)
				{
					html.AppendFormat("<li><a>{0}</a>", menu.Name);
					GenerateSubMenu(subItems, source, html);
					html.Append("</li>");
				}
				else
				{
					html.AppendFormat("<li><a href=\"{0}\" target=\"{1}\" rel=\"tab_{2}\">{3}</a>", menu.Href, menu.Target, menu.Id.ToLower(),
									menu.Name);
					html.Append("</li>");
				}
			}
			html.Append("</ul>");
		}

		public static MvcHtmlString GenerateMenu4Check(List<WebObject> webObjects, string selectedItemJsonString, string callback)
		{
			var selectedCodes =new List<string>();
			if (!string.IsNullOrWhiteSpace(selectedItemJsonString))
			{
				selectedCodes = JsonConvert.DeserializeObject<List<string>>(selectedItemJsonString);
			}
			var modules = webObjects.Where(x => x.WebObjectType == (int) WebObjectTypes.Module).OrderBy(x => x.OrderWeight).ToList();
			var html = new StringBuilder();
			if (!string.IsNullOrWhiteSpace(callback))
			{
				callback = "oncheck=\"" + callback + "\"";
			}
			else
			{
				callback = "";
			}
			html.AppendFormat("<ul class=\"tree treeFolder treeCheck expand\" {0}>", callback);
			foreach (var module in modules)
			{
				html.AppendFormat("<li><a tvalue=\"{0}\">{1}</a>", module.Id, module.Name);
				var menus = webObjects.Where(x => x.ParentId == module.Id).OrderBy(x => x.OrderWeight).ToList();
				GenerateSubMenu4Check(menus, webObjects, html, selectedCodes);
				html.Append("</li>");
			}
			html.Append("</ul>");
			return MvcHtmlString.Create(html.ToString());
		}

		protected static void GenerateSubMenu4Check(List<WebObject> menus, List<WebObject> source, StringBuilder html, List<string> selectedItems)
		{
			html.Append("<ul>");
			foreach (var menu in menus)
			{
				var subItems = source.Where(x => x.ParentId == menu.Id && x.WebObjectType == (int) WebObjectTypes.Menu).OrderBy(x => x.OrderWeight).ToList();
				if (subItems.Count > 0)
				{
					html.AppendFormat("<li><a tvalue=\"{0}\">{1}</a>", menu.Id, menu.Name);
					GenerateSubMenu4Check(subItems, source, html, selectedItems);
					html.Append("</li>");
				}
				else
				{
					var check = "";
					if (selectedItems.Count > 0 && selectedItems.Contains(menu.Id))
					{
						check = " checked=\"true\"";
					}
					html.AppendFormat("<li><a tvalue=\"{0}\"{1}>{2}</a>", menu.Id, check, menu.Name);
					html.Append("</li>");
				}
			}
			html.Append("</ul>");
		}
	}
}